package com.rimi.lzhg.service.impl;

import com.rimi.lzhg.dao.UserMapper;
import com.rimi.lzhg.entry.User;
import com.rimi.lzhg.entry.UserExample;
import com.rimi.lzhg.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public List<User> getUserList(User user) {
        return userMapper.selectByExample(null);
    }

    @Override
    public User login(User user) {
        UserExample userExample=new UserExample();
       userExample.createCriteria()
               .andCodeEqualTo(user.getCode())
               .andPasswordEqualTo(user.getPassword());
        List<User> users = userMapper.selectByExample(userExample);
        if(users!=null&&users.size()>0){
            return users.get(0);
        }
        return null;
    }

    @Override
    public boolean register(User user) {
        int i = userMapper.insertSelective(user);
        if(i>0){
            return true;
        }
        return false;
    }

    @Override
    public User getUserByCode(String code) {
        UserExample userExample=new UserExample();
        userExample.createCriteria().andCodeEqualTo(code);
        System.out.println(code);
        List<User> users = userMapper.selectByExample(userExample);
        System.out.println(users.size());
        if(users.size()>0){
            return users.get(0);
        }
        return null;
    }

    @Override
    public boolean updateUser(User user) {
        int i = userMapper.updateByPrimaryKeySelective(user);
        if(i>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteUserById(Integer id) {
        int i = userMapper.deleteByPrimaryKey(id);
        if(i>0){
            return true;
        }
        return false;
    }
}
